import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loading_animations/loading_animations.dart';
import 'package:mfkb/config/Config.dart';
import 'package:mfkb/pages/modules/MyDialog.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/FunService.dart';
import 'package:mfkb/service/UserService.dart';
import 'package:toast/toast.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  List<dynamic> weekKb = [] ;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  var today = {} ;

  int week = 4 ; // 当前是第几周
  int day = 1 ;

  bool isWeek = false ;

  @override
  void initState() {
    super.initState();

    DateTime now = DateTime.now() ;
    today["weekDay"] = now.weekday ;

    // 1.给weekKB生成结构
    for(var w = 0 ; w < 22 ; w++){
      var day = [] ;
      for(var i = 0 ; i < 7 ; i++){
        List temp = [] ;
        for(var j = 0 ; j < 5 ; j++){
          temp.add({}) ;
        }
        day.add(temp) ;
      }
      weekKb.add(day) ;
    }

    FunService().kb().then((value){
      if(value["code"] == 200){
        // print(value) ;
        for(var i = 0 ; i < value["data"]["kb"].length ; i++){
          List time = parseTime(value["data"]["kb"][i]) ;
          for(var w = 0 ; w < time.length ; w++){
            var temp = weekKb[time[w] - 1][value["data"]["kb"][i]["day"] - 1][value["data"]["kb"][i]["classtime"] - 1] ;
            temp["className"] = value["data"]["kb"][i]["className"] ;
            temp["teacherName"] = value["data"]["kb"][i]["teacherName"] ;
            temp["classroom"] = value["data"]["kb"][i]["classroom"] ;
          }
        }
        // 计算当前是第几周
        for(var i = 1 ; i <= value["data"]["timeTable"].length ; i++){
          DateTime temp = DateTime.parse(value["data"]["timeTable"][i.toString()]) ;
          if(now.isBefore(temp)){
            week = i - 1 ;
            break ;
          }
        }
        today["week"] = week ;
        setState(() {});
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors : [Colors.white , Color.fromARGB(255, 246, 246, 248)] ,
            begin: Alignment.topCenter ,
            end:  Alignment.bottomCenter
          )
        ),
        child: isWeek ? KbAll(weekKb: weekKb,week: week , scaffoldKey: _scaffoldKey,onChange: (){
          setState(() {
            isWeek = false ;
          });
        },weekChange:(value){
          setState(() {
            week = value ;
          }) ;
        }) : KbWeek(weekKb: weekKb,week: week , today: today , onChange: (){
          setState(() {
            isWeek = true ;
          });
        },) ,
      ),
    );
  }

  List parseTime(dynamic data){
    var timeData = data["classZc"] ;
    var temp = [] ;
    var flag = 1 ; //1表示普通 ， 2表示单周 ， 3表示双周
    if(timeData.indexOf("单周") != -1){
      flag = 2 ;
    }else if(timeData.indexOf("双周") != -1){
      flag = 3 ;
    }
    timeData = timeData.replaceAll("/[\u4e00-\u9fa5]/" , "").replaceAll("(" , "").replaceAll(")" , "") ;
    RegExp exp = new RegExp(r"([\u4e00-\u9fa5])");
    Iterable<Match> matches = exp.allMatches(timeData);
    for (Match m in matches) {
      String match = m.group(0);
      timeData = timeData.replaceAll(match , "") ;
    }
    //先以逗号切割
    var t1 = timeData.split(",") ;
    for(var i = 0 ; i < t1.length ; i++){
      if(t1[i].indexOf("-") != -1){
        int start = int.parse(t1[i].split("-")[0]) ;
        int end   = int.parse(t1[i].split("-")[1]) ;
        for(var j=start*1 ; j<= end*1 ; j++){
          if(flag == 1){
            temp.add(j*1) ;
          }else if(flag == 2){
            if(j %2 == 1){
              temp.add(j) ;
            }
          }else if(flag == 3){
            if(j %2 == 0){
              temp.add(j) ;
            }
          }
        }
      }else{
        temp.add(int.parse(t1[i])) ;
      }
    }
    return temp ;
  }
}

class KbAll extends StatefulWidget {
  dynamic weekKb ;
  int week  ;
  dynamic onChange ;
  GlobalKey<ScaffoldState> scaffoldKey ;
  dynamic weekChange ;

  KbAll({Key key , @required this.weekKb , @required this.week , @required this.scaffoldKey , @required this.onChange , @required this.weekChange}):super(key : key) ;

  @override
  _KbAllState createState() => _KbAllState();
}

class _KbAllState extends State<KbAll> {

  String date = "" ;
  List weekCH = ["一" , "二" , "三" , "四" , "五" , "六" , "七" , "八" , "九" , "十" , "十一" , "十二" , "十三" , "十四" , "十五" , "十六" , "十七" , "十八" , "十九" , "二十" , "二十一" , "二十二" , "二十三" , "二十四" , "二十五"] ;

  @override
  void initState() {
    super.initState();
    DateTime now = DateTime.now() ;
    date = now.year.toString()+"/"+now.month.toString()+"月" ;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).padding.top 
          ) ,
          Expanded(
            child: ScrollConfiguration(
              behavior: MyBehavior() ,
              child: ListView(
                padding: EdgeInsets.all(0),
                children: <Widget>[
                  Container(
                    height: ScreenUtil().setHeight(33),
                    margin: EdgeInsets.only(top:ScreenUtil().setHeight(14)),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          flex: 2,
                          child: GestureDetector(
                            child: Container(
                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                              color: Color.fromARGB(0, 0, 0, 0),
                              child: Text("第"+weekCH[widget.week]+"周" , style: TextStyle(
                                fontSize: ScreenUtil().setSp(24) ,
                                fontWeight: FontWeight.bold , 
                                color: Color.fromARGB(255, 19, 28, 53) ,
                                height: ScreenUtil().setHeight(33) / ScreenUtil().setSp(24)
                              ),),
                            ),
                            onTap: (){
                               new Picker(
                                adapter: PickerDataAdapter<String>(
                                  data: List.generate(22, (index){
                                    return PickerItem(
                                      text: Text("第"+weekCH[index]+"周") ,
                                      value: index.toString()
                                    );
                                  }) 
                                ),
                                confirmText: "确认",
                                confirmTextStyle: TextStyle(
                                  fontSize: ScreenUtil().setSp(16) ,
                                  color: Color.fromARGB(255, 0, 195, 206)
                                ),
                                cancelText: "取消" ,
                                cancelTextStyle: TextStyle(
                                  fontSize: ScreenUtil().setSp(16) ,
                                  color: Color.fromARGB(255, 0, 195, 206)
                                ),
                                changeToFirst: true ,
                                textScaleFactor: 1.3 ,
                                itemExtent:  35 ,
                                textAlign: TextAlign.left , 
                                textStyle: const TextStyle(
                                  color: Color.fromARGB(255, 51, 51, 51) , 
                                  height: 35 / 13 ,
                                  fontSize: 13
                                ),
                                selectedTextStyle: TextStyle(color: Color.fromARGB(255, 51, 51, 51)),
                                columnPadding: const EdgeInsets.all(8.0), 
                                onConfirm: (Picker picker, List value) {
                                  if(widget.weekChange != null){
                                    print(value[0]) ;
                                    widget.weekChange(value[0]) ;
                                  }
                                }
                              ).show(widget.scaffoldKey.currentState) ;
                            },
                          ),
                        ) ,
                        Expanded(
                          flex: 1,
                          child: Container(
                            margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: <Widget>[
                                GestureDetector(
                                  child: Container(
                                    width: ScreenUtil().setHeight(33),
                                    height: ScreenUtil().setHeight(33),
                                    child: Image.asset("assets/images/date.png"),
                                  ),
                                  onTap: (){
                                    if(widget.onChange != null){
                                      widget.onChange() ;
                                    }
                                  },
                                )
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ) ,
                  Container(
                    height: ScreenUtil().setHeight(20),
                    margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                    child: Text(date , style: TextStyle(
                      fontSize: ScreenUtil().setSp(14) ,
                      color: Color.fromARGB(255, 127, 135, 159) ,
                      height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(14)
                    ),),
                  ) ,
                  Container(
                    child: Row(
                      children: <Widget>[
                        Container(
                          width: ScreenUtil().setWidth(45),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(2)),
                          child: Column(
                            children: List.generate(7, (index){
                              if(index == 0){
                                return Container(
                                  height: ScreenUtil().setHeight(82),
                                ) ;
                              }else if(index == 1){ // 分割线
                                return Container(
                                  height: 1,
                                  color: Color.fromARGB(255, 236, 237, 241),
                                ) ;
                              }else{
                                return Container(
                                  height: ScreenUtil().setHeight(110),
                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(index == 2 ? 15 :12)),
                                  child: Center(
                                    child: Container(
                                      width: ScreenUtil().setWidth(14),
                                      child: Text("第"+weekCH[index - 2]+"节" , style: TextStyle(
                                        fontSize: ScreenUtil().setSp(14) ,
                                        color: Color.fromARGB(255, 78, 86, 108)
                                      ),),
                                    ),
                                  ),
                                ) ;
                              }
                            }),
                          ),
                        ) ,
                        Container(
                          width: ScreenUtil().setWidth(65),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(2)),
                          child: Column(
                            children: List.generate(7, (index){
                              if(index == 0){
                                return Container(
                                  height: ScreenUtil().setHeight(82),
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(7)),
                                  child: Container(
                                    child: Container(
                                      width: ScreenUtil().setWidth(65),
                                      margin: EdgeInsets.only(top:ScreenUtil().setHeight(22) , bottom:ScreenUtil().setHeight(22)),
                                      decoration: BoxDecoration(
                                        border:Border.all(
                                          color: Color.fromARGB(255, 236, 237, 241) ,
                                        ) ,
                                        borderRadius:BorderRadius.all(Radius.circular(8)) ,
                                        color: Colors.white
                                      ),
                                      child: Center(
                                        child: Text("周"+weekCH[0] , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          color: Color.fromARGB(255, 78, 86, 108) ,
                                          fontWeight: FontWeight.bold 
                                        ),),
                                      ),
                                    ),
                                  ),
                                ) ;
                              }else if(index == 1){ // 分割线
                                return Container(
                                  height: 1,
                                  color: Color.fromARGB(255, 236, 237, 241),
                                ) ;
                              }else{
                                return Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(index == 2 ? 15 :12)),
                                  height: ScreenUtil().setHeight(110),
                                  child: Container(
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: widget.weekKb[widget.week][0][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Color.fromARGB(255, 236, 237, 241)
                                      ) ,
                                      color: widget.weekKb[widget.week][0][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Colors.white ,
                                      borderRadius: BorderRadius.all(Radius.circular(8))
                                    ),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start ,
                                      children: <Widget>[
                                        Container(
                                          height: ScreenUtil().setHeight(54),
                                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(9) , left:ScreenUtil().setWidth(7) , right:ScreenUtil().setWidth(7)),
                                          child: Text(widget.weekKb[widget.week][0][index - 2]["className"] == null ? "" : widget.weekKb[widget.week][0][index - 2]["className"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(13) ,
                                            color: Color.fromARGB(255, 0, 167, 187) ,
                                            fontWeight: FontWeight.bold ,
                                            height: ScreenUtil().setHeight(17) / ScreenUtil().setSp(13)
                                          ),overflow: TextOverflow.ellipsis,maxLines: 3,),
                                        ) ,
                                        Container(
                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , right:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(13)),
                                          height: ScreenUtil().setHeight(14),
                                          child: Text(widget.weekKb[widget.week][0][index - 2]["classroom"] == null ? "" : widget.weekKb[widget.week][0][index - 2]["classroom"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(10) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(10)
                                          ),overflow: TextOverflow.ellipsis , textAlign: TextAlign.left,),
                                        )
                                      ],
                                    ),
                                  ),
                                ) ;
                              }
                            }),
                          ),
                        ) ,
                        Container(
                          width: ScreenUtil().setWidth(65),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(2)),
                          child: Column(
                            children: List.generate(7, (index){
                              if(index == 0){
                                return Container(
                                  height: ScreenUtil().setHeight(82),
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(7)),
                                  child: Container(
                                    child: Container(
                                      width: ScreenUtil().setWidth(65),
                                      margin: EdgeInsets.only(top:ScreenUtil().setHeight(22) , bottom:ScreenUtil().setHeight(22)),
                                      decoration: BoxDecoration(
                                        border:Border.all(
                                          color: Color.fromARGB(255, 236, 237, 241) ,
                                        ) ,
                                        borderRadius:BorderRadius.all(Radius.circular(8)) ,
                                      ),
                                      child: Center(
                                        child: Text("周"+weekCH[1] , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          color: Color.fromARGB(255, 78, 86, 108) ,
                                          fontWeight: FontWeight.bold 
                                        ),),
                                      ),
                                    ),
                                  ),
                                ) ;
                              }else if(index == 1){ // 分割线
                                return Container(
                                  height: 1,
                                  color: Color.fromARGB(255, 236, 237, 241),
                                ) ;
                              }else{
                                return Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(index == 2 ? 15 :12)),
                                  height: ScreenUtil().setHeight(110),
                                  child: Container(
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: widget.weekKb[widget.week][1][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Color.fromARGB(255, 236, 237, 241)
                                      ) ,
                                      color: widget.weekKb[widget.week][1][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Colors.white ,
                                      borderRadius: BorderRadius.all(Radius.circular(8))
                                    ),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start ,
                                      children: <Widget>[
                                        Container(
                                          height: ScreenUtil().setHeight(54),
                                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(9) , left:ScreenUtil().setWidth(7) , right:ScreenUtil().setWidth(7)),
                                          child: Text(widget.weekKb[widget.week][1][index - 2]["className"] == null ? "" : widget.weekKb[widget.week][1][index - 2]["className"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(13) ,
                                            color: Color.fromARGB(255, 0, 167, 187) ,
                                            fontWeight: FontWeight.bold ,
                                            height: ScreenUtil().setHeight(17) / ScreenUtil().setSp(13)
                                          ),overflow: TextOverflow.ellipsis,maxLines: 3,),
                                        ) ,
                                        Container(
                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , right:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(13)),
                                          height: ScreenUtil().setHeight(14),
                                          child: Text(widget.weekKb[widget.week][1][index - 2]["classroom"] == null ? "" : widget.weekKb[widget.week][1][index - 2]["classroom"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(10) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(10)
                                          ),overflow: TextOverflow.ellipsis , textAlign: TextAlign.left,),
                                        )
                                      ],
                                    ),
                                  ),
                                ) ;
                              }
                            }),
                          ),
                        ) ,
                        Container(
                          width: ScreenUtil().setWidth(65),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(2)),
                          child: Column(
                            children: List.generate(7, (index){
                              if(index == 0){
                                return Container(
                                  height: ScreenUtil().setHeight(82),
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(7)),
                                  child: Container(
                                    child: Container(
                                      width: ScreenUtil().setWidth(65),
                                      margin: EdgeInsets.only(top:ScreenUtil().setHeight(22) , bottom:ScreenUtil().setHeight(22)),
                                      decoration: BoxDecoration(
                                        border:Border.all(
                                          color: Color.fromARGB(255, 236, 237, 241) ,
                                        ) ,
                                        borderRadius:BorderRadius.all(Radius.circular(8)) ,
                                      ),
                                      child: Center(
                                        child: Text("周"+weekCH[2] , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) , 
                                          color: Color.fromARGB(255, 78, 86, 108) ,
                                          fontWeight: FontWeight.bold 
                                        ),),
                                      ),
                                    ),
                                  ),
                                ) ;
                              }else if(index == 1){ // 分割线
                                return Container(
                                  height: 1,
                                  color: Color.fromARGB(255, 236, 237, 241),
                                ) ;
                              }else{
                                return Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(index == 2 ? 15 :12)),
                                  height: ScreenUtil().setHeight(110),
                                  child: Container(
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: widget.weekKb[widget.week][2][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Color.fromARGB(255, 236, 237, 241)
                                      ) ,
                                      color: widget.weekKb[widget.week][2][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Colors.white ,
                                      borderRadius: BorderRadius.all(Radius.circular(8))
                                    ),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start ,
                                      children: <Widget>[
                                        Container(
                                          height: ScreenUtil().setHeight(54),
                                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(9) , left:ScreenUtil().setWidth(7) , right:ScreenUtil().setWidth(7)),
                                          child: Text(widget.weekKb[widget.week][2][index - 2]["className"] == null ? "" : widget.weekKb[widget.week][2][index - 2]["className"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(13) ,
                                            color: Color.fromARGB(255, 0, 167, 187) ,
                                            fontWeight: FontWeight.bold ,
                                            height: ScreenUtil().setHeight(17) / ScreenUtil().setSp(13)
                                          ),overflow: TextOverflow.ellipsis,maxLines: 3,),
                                        ) ,
                                        Container(
                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , right:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(13)),
                                          height: ScreenUtil().setHeight(14),
                                          child: Text(widget.weekKb[widget.week][2][index - 2]["classroom"] == null ? "" : widget.weekKb[widget.week][2][index - 2]["classroom"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(10) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(10)
                                          ),overflow: TextOverflow.ellipsis , textAlign: TextAlign.left,),
                                        )
                                      ],
                                    ),
                                  ),
                                ) ;
                              }
                            }),
                          ),
                        ) ,
                        Container(
                          width: ScreenUtil().setWidth(65),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(2)),
                          child: Column(
                            children: List.generate(7, (index){
                              if(index == 0){
                                return Container(
                                  height: ScreenUtil().setHeight(82),
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(7)),
                                  child: Container(
                                    child: Container(
                                      width: ScreenUtil().setWidth(65),
                                      margin: EdgeInsets.only(top:ScreenUtil().setHeight(22) , bottom:ScreenUtil().setHeight(22)),
                                      decoration: BoxDecoration(
                                        border:Border.all(
                                          color: Color.fromARGB(255, 236, 237, 241) ,
                                        ) ,
                                        borderRadius:BorderRadius.all(Radius.circular(8)) ,
                                      ),
                                      child: Center(
                                        child: Text("周"+weekCH[3] , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          color: Color.fromARGB(255, 78, 86, 108) ,
                                          fontWeight: FontWeight.bold 
                                        ),),
                                      ),
                                    ),
                                  ),
                                ) ;
                              }else if(index == 1){ // 分割线
                                return Container(
                                  height: 1,
                                  color: Color.fromARGB(255, 236, 237, 241),
                                ) ;
                              }else{
                                return Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(index == 2 ? 15 :12)),
                                  height: ScreenUtil().setHeight(110),
                                  child: Container(
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: widget.weekKb[widget.week][3][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Color.fromARGB(255, 236, 237, 241)
                                      ) ,
                                      color: widget.weekKb[widget.week][3][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Colors.white ,
                                      borderRadius: BorderRadius.all(Radius.circular(8))
                                    ),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start ,
                                      children: <Widget>[
                                        Container(
                                          height: ScreenUtil().setHeight(54),
                                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(9) , left:ScreenUtil().setWidth(7) , right:ScreenUtil().setWidth(7)),
                                          child: Text(widget.weekKb[widget.week][3][index - 2]["className"] == null ? "" : widget.weekKb[widget.week][3][index - 2]["className"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(13) ,
                                            color: Color.fromARGB(255, 0, 167, 187) ,
                                            fontWeight: FontWeight.bold ,
                                            height: ScreenUtil().setHeight(17) / ScreenUtil().setSp(13)
                                          ),overflow: TextOverflow.ellipsis,maxLines: 3,),
                                        ) ,
                                        Container(
                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , right:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(13)),
                                          height: ScreenUtil().setHeight(14),
                                          child: Text(widget.weekKb[widget.week][3][index - 2]["classroom"] == null ? "" : widget.weekKb[widget.week][3][index - 2]["classroom"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(10) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(10)
                                          ),overflow: TextOverflow.ellipsis , textAlign: TextAlign.left,),
                                        )
                                      ],
                                    ),
                                  ),
                                ) ;
                              }
                            }),
                          ),
                        ) ,
                        Container(
                          width: ScreenUtil().setWidth(65),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(2)),
                          child: Column(
                            children: List.generate(7, (index){
                              if(index == 0){
                                return Container(
                                  height: ScreenUtil().setHeight(82),
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(7)),
                                  child: Container(
                                    child: Container(
                                      width: ScreenUtil().setWidth(65),
                                      margin: EdgeInsets.only(top:ScreenUtil().setHeight(22) , bottom:ScreenUtil().setHeight(22)),
                                      decoration: BoxDecoration(
                                        border:Border.all(
                                          color: Color.fromARGB(255, 236, 237, 241) ,
                                        ) ,
                                        borderRadius:BorderRadius.all(Radius.circular(8)) ,
                                      ),
                                      child: Center(
                                        child: Text("周"+weekCH[4] , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          color: Color.fromARGB(255, 78, 86, 108) ,
                                          fontWeight: FontWeight.bold 
                                        ),),
                                      ),
                                    ),
                                  ),
                                ) ;
                              }else if(index == 1){ // 分割线
                                return Container(
                                  height: 1,
                                  color: Color.fromARGB(255, 236, 237, 241),
                                ) ;
                              }else{
                                return Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(index == 2 ? 15 :12)),
                                  height: ScreenUtil().setHeight(110),
                                  child: Container(
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: widget.weekKb[widget.week][4][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Color.fromARGB(255, 236, 237, 241)
                                      ) ,
                                      color: widget.weekKb[widget.week][4][index - 2].length == 0 ? Color.fromARGB(0, 0, 0, 0) : Colors.white ,
                                      borderRadius: BorderRadius.all(Radius.circular(8))
                                    ),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start ,
                                      children: <Widget>[
                                        Container(
                                          height: ScreenUtil().setHeight(54),
                                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(9) , left:ScreenUtil().setWidth(7) , right:ScreenUtil().setWidth(7)),
                                          child: Text(widget.weekKb[widget.week][4][index - 2]["className"] == null ? "" : widget.weekKb[widget.week][4][index - 2]["className"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(13) ,
                                            color: Color.fromARGB(255, 0, 167, 187) ,
                                            fontWeight: FontWeight.bold ,
                                            height: ScreenUtil().setHeight(17) / ScreenUtil().setSp(13)
                                          ),overflow: TextOverflow.ellipsis,maxLines: 3,),
                                        ) ,
                                        Container(
                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(5) , right:ScreenUtil().setWidth(5) , top:ScreenUtil().setHeight(13)),
                                          height: ScreenUtil().setHeight(14),
                                          child: Text(widget.weekKb[widget.week][4][index - 2]["classroom"] == null ? "" : widget.weekKb[widget.week][4][index - 2]["classroom"] , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(10) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(10)
                                          ),overflow: TextOverflow.ellipsis , textAlign: TextAlign.left,),
                                        )
                                      ],
                                    ),
                                  ),
                                ) ;
                              }
                            }),
                          ),
                        ) ,
                      ],
                    ),
                  )
                ]
              )
            )
          )
        ]
      )
    );
  }
}

class KbWeek extends StatefulWidget {

  dynamic weekKb ;
  int week  ;
  dynamic onChange ;
  dynamic today ;

  KbWeek({Key key , @required this.weekKb , @required this.week , @required this.today , @required this.onChange}):super(key : key) ;

  @override
  _KbWeekState createState() => _KbWeekState();
}

class _KbWeekState extends State<KbWeek> with SingleTickerProviderStateMixin  {

  dynamic head_tip = [{"name":"一" , "time":"08:00-10:00"} , {"name":"二" , "time":"10:00-12:00"} , {"name":"三" , "time":"14:00-16:00"} , {"name":"四" , "time":"16:00-18:00"} , {"name":"五" , "time":"19:00-21:00"}] ;
  String date = "" ;
  List<dynamic> head = [{"name":"一" , "date":1 , "selected":false}, {"name":"二" , "date":2, "selected":false} ,{"name":"三" , "date":3, "selected":false} ,{"name":"四" , "date":4, "selected":false} ,{"name":"五" , "date":5, "selected":false} ,{"name":"六" , "date":6, "selected":false} ,{"name":"日" , "date":7, "selected":false}] ;
  bool isSpread = false ;
  FocusNode focusNode ;
  bool flag = false ;
  String content = "" ;
  List flags = [] ;
  bool loading = false ;
  bool mode = false ;
  int current = 0 ;

  // 定义动画
  Animation<double> animation;
  AnimationController controller;

  var pos = {"start":{"x":0.0 , "y":0.0}} ;
  bool isDrag = false ;

  int day ;

  @override
  void initState() {
    super.initState();
    if(Config().getSpf().getString("tasks") != null){
      flags = jsonDecode(Config().getSpf().get("tasks")) ;
    }
    focusNode = new FocusNode() ;
    DateTime now = DateTime.now() ;
    date = now.year.toString()+"/"+now.month.toString()+"月" ;
    var weekDay = now.weekday ;
    day = weekDay ;
    head[weekDay - 1]["selected"] = true ;
    for(var i = 0 ; i < head.length ; i++){
      DateTime temp = now.add(Duration(days: 1)*(i - weekDay + 1)) ;
      head[i]["date"] = temp.day ;
    }

    // 初始化动画
    controller = new AnimationController(
    duration: const Duration(milliseconds: 500), vsync: this);
    animation = CurvedAnimation(parent: controller, curve: Curves.easeIn);
    //图片宽高从0变到300
    animation = new Tween(begin: 0.0, end: 289.0).animate(animation)
    ..addListener(() {
      setState(()=>{});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Column(
          children: <Widget>[
            Container(
              height: MediaQuery.of(context).padding.top 
            ) ,
            Expanded(
              child: GestureDetector(
                child: Container(
                  child: ScrollConfiguration(
                    behavior: MyBehavior() ,
                    child: ListView(
                      padding: EdgeInsets.all(0),
                      children: <Widget>[
                        Container(
                          height: ScreenUtil().setHeight(33),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(14)),
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                flex: 2,
                                child: Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                  child: Text("今日课程" , style: TextStyle(
                                    fontSize: ScreenUtil().setSp(24) ,
                                    fontWeight: FontWeight.bold ,
                                    color: Color.fromARGB(255, 19, 28, 53) ,
                                    height: ScreenUtil().setHeight(33) / ScreenUtil().setSp(24)
                                  ),),
                                ),
                              ) ,
                              Expanded(
                                flex: 1,
                                child: Container(
                                  margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: <Widget>[
                                      GestureDetector(
                                        child: Container(
                                          width: ScreenUtil().setHeight(33),
                                          height: ScreenUtil().setHeight(33),
                                          child: Image.asset("assets/images/date.png"),
                                        ),
                                        onTap: (){
                                          if(widget.onChange != null){
                                            widget.onChange() ;
                                          }
                                        },
                                      )
                                    ],
                                  ),
                                ),
                              )
                            ],
                          ),
                        ) ,
                        Container(
                          height: ScreenUtil().setHeight(20),
                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                          child: Text(date , style: TextStyle(
                            fontSize: ScreenUtil().setSp(14) ,
                            color: Color.fromARGB(255, 127, 135, 159) ,
                            height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(14)
                          ),),
                        ) ,
                        Container(
                          height: ScreenUtil().setHeight(58),
                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(16) , right:ScreenUtil().setWidth(16) , top:ScreenUtil().setHeight(24)),
                          child: Row(
                            children: List.generate(head.length, (index){
                              return GestureDetector(
                                child: Container(
                                  width: ScreenUtil().setWidth(34),
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(index == 0 ? 0 : 17)),
                                  color: Color.fromARGB(0, 0, 0, 0),
                                  child: Column(
                                    children: <Widget>[
                                      Container(
                                        height: ScreenUtil().setHeight(16),
                                        margin: EdgeInsets.only(bottom:ScreenUtil().setHeight(8)),
                                        child: Center(
                                          child: Text(head[index]["name"] , style: TextStyle(
                                            height: ScreenUtil().setHeight(16) / ScreenUtil().setSp(12) ,
                                            fontSize: ScreenUtil().setSp(12) ,
                                            color: Color.fromARGB(255, 121, 132, 155) ,
                                            fontWeight: FontWeight.bold 
                                          ),),
                                        ),
                                      ) ,
                                      Container(
                                        // margin: EdgeInsets.only(top:ScreenUtil().setHeight(8)),
                                        width: ScreenUtil().setHeight(34),
                                        height: ScreenUtil().setHeight(34),
                                        decoration: BoxDecoration(
                                          color: index == day - 1 ? Color.fromARGB(255, 0, 195, 206) : Color.fromARGB(0, 0, 0, 0) ,
                                          borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setHeight(17))) ,
                                          boxShadow: List.generate(index == day - 1 ? 1 : 0, (i){
                                            return BoxShadow(
                                              blurRadius: 3 ,
                                              color: Color.fromARGB(110, 19, 28, 53) ,
                                              spreadRadius: 0
                                            ) ;
                                          })
                                        ),
                                        child: Center(
                                          child: Text(head[index]["date"].toString() , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(17) ,
                                            fontWeight: FontWeight.bold ,
                                            color: index == day - 1 ? Colors.white : Color.fromARGB(255, 19, 28, 53)
                                          ),),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                onTap: (){
                                  for(var i = 0 ; i < head.length ; i++){
                                    head[i]["selected"] = false ;
                                  }
                                  head[index]["selected"] = true ;
                                  day = index + 1 ;
                                  setState(() {});
                                },
                              ) ;
                            }),
                          ),
                        ) ,
                        Container(
                          child: Container(
                            margin: EdgeInsets.only(top:ScreenUtil().setHeight(15) , left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(15)),
                            child: Column(
                              children: [
                                Column(
                                  children: List.generate(widget.weekKb[widget.week - 1][day - 1].length, (index){
                                    return widget.weekKb[widget.week - 1][day - 1][index].length == 0 ? Container(
                                      // margin: EdgeInsets.only(top:ScreenUtil().setHeight(8)),
                                      // height: ScreenUtil().setHeight(78),
                                      // decoration: BoxDecoration(
                                      //   color: Color.fromARGB(0, 0, 0, 0) ,
                                      //   borderRadius: BorderRadius.all(Radius.circular(12)) ,
                                      //   border: Border.all(
                                      //     color: Color.fromARGB(255, 246, 246, 248)
                                      //   )
                                      // ),
                                    ) :  Container(
                                      margin: EdgeInsets.only(top:ScreenUtil().setHeight(8)),
                                      height: ScreenUtil().setHeight(78),
                                      decoration: BoxDecoration(
                                        color: Colors.white ,
                                        borderRadius: BorderRadius.all(Radius.circular(12)) ,
                                        border: Border.all(
                                          color: Color.fromARGB(255, 246, 246, 248)
                                        )
                                      ),
                                      child: Row(
                                        children: <Widget>[
                                          Container(
                                            width: ScreenUtil().setWidth(104),
                                            height: ScreenUtil().setHeight(40),
                                            color: Color.fromARGB(0, 0, 0, 0),
                                            child: Column( 
                                              mainAxisAlignment: MainAxisAlignment.start,
                                              children: <Widget>[
                                                Container(
                                                  height: ScreenUtil().setHeight(22),
                                                  child: Text("第"+head_tip[index]["name"]+"节课" , style: TextStyle(
                                                    fontSize: ScreenUtil().setSp(16) ,
                                                    color: Color.fromARGB(255, 0, 167, 178) ,
                                                    height: ScreenUtil().setHeight(22) / ScreenUtil().setSp(16)
                                                  ),),
                                                ) ,
                                                Container(
                                                  height: ScreenUtil().setHeight(12),
                                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(6)),
                                                  child: Text(head_tip[index]["time"].toString() , style: TextStyle(
                                                    fontSize: ScreenUtil().setSp(12) ,
                                                    color: Color.fromARGB(255, 78 , 86 , 108) ,
                                                    height: ScreenUtil().setHeight(12) / ScreenUtil().setSp(12)
                                                  ),),
                                                )
                                              ],
                                            ),
                                          ) ,
                                          Container(
                                            width: ScreenUtil().setWidth(1),
                                            height: ScreenUtil().setHeight(45),
                                            color: Color.fromARGB(255, 236, 237, 241),
                                          ) ,
                                          Expanded(
                                            child: Container(
                                              height: ScreenUtil().setHeight(39),
                                              margin: EdgeInsets.only(left:ScreenUtil().setWidth(13)),
                                              child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start ,
                                                children: <Widget>[
                                                  Container(
                                                    height: ScreenUtil().setHeight(18),
                                                    child: Text(widget.weekKb[widget.week - 1][day - 1][index]["className"] , overflow: TextOverflow.ellipsis,style: TextStyle(
                                                      fontSize: ScreenUtil().setSp(14) ,
                                                      color: Color.fromARGB(255, 19, 28, 53) ,
                                                      fontWeight: FontWeight.bold ,
                                                      height: ScreenUtil().setHeight(18) / ScreenUtil().setSp(14)
                                                    ),),
                                                  ) ,
                                                  Container(
                                                    height: ScreenUtil().setHeight(14),
                                                    margin: EdgeInsets.only(top:ScreenUtil().setHeight(7)),
                                                    child: Row(
                                                      children: <Widget>[
                                                        Container(
                                                          width: ScreenUtil().setHeight(11),
                                                          height: ScreenUtil().setHeight(11),
                                                          child: Image.asset("assets/images/position.png" , fit:BoxFit.fill),
                                                        ) ,
                                                        Container(
                                                          height: ScreenUtil().setHeight(14),
                                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(5)),
                                                          child: Text(widget.weekKb[widget.week - 1][day - 1][index]["classroom"] , overflow: TextOverflow.ellipsis,style: TextStyle(
                                                            fontSize: ScreenUtil().setSp(12) ,
                                                            color: Color.fromARGB(255, 78 , 86 , 108) ,
                                                            height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(12)
                                                          ),),
                                                        )
                                                      ],
                                                    ),
                                                  )
                                                ],
                                              ),
                                            ),
                                          )
                                        ],
                                      ),
                                    );
                                  }),
                                ) ,
                                Column(
                                  children: List.generate(flags.length, (index){
                                    return  Visibility(
                                      visible: tasks(index) , 
                                      child: GestureDetector(
                                        child: Container(
                                          height:ScreenUtil().setHeight(78) ,
                                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(8)),
                                          decoration: BoxDecoration(
                                            color: Colors.white ,
                                            borderRadius: BorderRadius.all(Radius.circular(12)) ,
                                            border: Border.all(
                                              color: Color.fromARGB(255, 246, 246, 248)
                                            )
                                          ),
                                          child: Row(
                                            children: <Widget>[
                                              GestureDetector(
                                                child: Container(
                                                  width: ScreenUtil().setWidth(22),
                                                  height: ScreenUtil().setWidth(22),
                                                  color: Color.fromARGB(0, 0, 0, 0),
                                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(32) , right:ScreenUtil().setWidth(28)) ,
                                                  child: Image.asset(flags[index]["status"] == 0 ? "assets/images/un_select.png" : "assets/images/selected.png" , fit: BoxFit.fill,),
                                                ),
                                                onTap: (){
                                                  var status = flags[index]["status"] == 0 ? 1 : 0 ;
                                                  UserService().changeStatus(flags[index]["id"].toString(), status.toString()).then((value){
                                                    if(value["code"] == 200){
                                                      setState(() {
                                                        flags[index]["status"] = status ;
                                                        flags[index]["week"] = widget.today["week"] ;
                                                        flags[index]["day"] = widget.today["weekDay"] ;
                                                      });
                                                    }else{
                                                      Toast.show(value["msg"], context) ;
                                                    }
                                                  }) ;
                                                },
                                              ) ,
                                              Container(
                                                width: 1,
                                                height: ScreenUtil().setHeight(45), 
                                                color: Color.fromARGB(255, 236, 237, 241),
                                              ) ,
                                              Expanded(
                                                child: Container(
                                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(17) , right:ScreenUtil().setWidth(18)),
                                                  child: Text(flags[index]["content"] , maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle(
                                                    fontSize: ScreenUtil().setSp(14) ,
                                                    color: Color.fromARGB(255, 19, 28, 53) ,
                                                    decoration: flags[index]["status"] == 0 ? TextDecoration.none : TextDecoration.lineThrough
                                                  ),),
                                                ),
                                              )
                                            ],
                                          ),
                                        ),
                                        onTap: (){
                                          print(flags) ;
                                          if(flags[index]["status"] == 0){
                                            flag = true ;
                                            current = index ;
                                            content = flags[index]["content"] ;
                                            mode = true ;
                                          }
                                          setState(() {});
                                        },
                                        onLongPress: () async{
                                          var m = 0 ;
                                          print(flags[index]["id"]) ;
                                          await showDialog(
                                            context: context ,
                                            child: MyDialog(no: (){}, yes: (){
                                              UserService().delTasks(flags[index]["id"]).then((value){
                                                if(value["code"] == 200){
                                                  flags.removeAt(index) ;
                                                  setState(() {});
                                                  m = 1 ;
                                                  Config().getSpf().setString("tasks", jsonEncode(flags)) ;
                                                }else{
                                                  Toast.show(value["msg"], context , gravity: 1) ;
                                                }
                                              }) ;
                                              Navigator.of(context).pop() ;
                                            }, title: "确认删除",)
                                          ) ;
                                          if(m == 1){
                                            return true ;
                                          }
                                        },
                                      ),
                                    ) ;
                                  }),
                                )
                              ],
                            ),
                          )
                        )
                      ],
                    ),
                  ),
                ),
                onHorizontalDragDown: (e){
                  pos["start"]["x"] = e.globalPosition.dx ;
                  pos["start"]["y"] = e.globalPosition.dy ;
                  isDrag = true ;
                },
                onHorizontalDragUpdate: (e){
                  if(isDrag){
                    var x = e.globalPosition.dx ;
                    var y = e.globalPosition.dy ;
                    var dx = pos["start"]["x"] - x ;
                    var dy = pos["start"]["y"] - y ;
                    if(dx.abs() > ScreenUtil().setWidth(375)*0.2 && dx.abs() > dy.abs()) {
                      if(dx > 0){
                        if(day == 7) return ;
                        setState(() {
                          day = day + 1 ;
                        });
                      }else{
                        if(day == 1) return ;
                        setState(() {
                          day = day - 1 ;
                        });
                      }
                      isDrag = false ;
                    }
                  }
                },
                onHorizontalDragCancel: (){
                  isDrag = false ;
                }
              ),
            )
          ],
        ) ,
        // Positioned(
        //   right: ScreenUtil().setWidth(15),
        //   bottom: ScreenUtil().setHeight(24),
        //   left: ScreenUtil().setWidth(isSpread ? 304 - animation.value : 304) ,
        //   child: Container(
        //     height: ScreenUtil().setWidth(56) ,
        //     decoration: BoxDecoration(
        //       borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setWidth(28))) ,
        //       color: Colors.white ,
        //       boxShadow: [
        //         BoxShadow(
        //           color: Color.fromARGB(20, 0, 0, 0) ,
        //           blurRadius: 4,
        //           spreadRadius: 0
        //         )
        //       ]
        //     ),
        //     child: Row(
        //       children: <Widget>[
        //         Visibility(
        //           visible: isSpread, 
        //           child: Container(
        //             width: ScreenUtil().setWidth(animation.value) , 
        //             decoration: BoxDecoration(
        //               borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setWidth(28))) ,
        //               color: Colors.white 
        //             ),
        //             child: Row(
        //               children: <Widget>[
        //                 Visibility(
        //                   visible: animation.value > ScreenUtil().setWidth(70),
        //                   child: Container(
        //                     width: ScreenUtil().setWidth(32),
        //                     height: ScreenUtil().setWidth(48),
        //                     margin: EdgeInsets.only(left:ScreenUtil().setWidth(41)),
        //                     child: Column(
        //                       children: <Widget>[
        //                         Container(
        //                           width: ScreenUtil().setWidth(32),
        //                           height: ScreenUtil().setWidth(32),
        //                           child: Image.asset("assets/images/renwu.png"),
        //                         ) ,
        //                         Container(
        //                           height: ScreenUtil().setWidth(14),
        //                           child: Text("任务" , style: TextStyle(
        //                             fontSize: ScreenUtil().setWidth(10) ,
        //                             color: Color.fromARGB(255, 121, 132, 155) ,
        //                             height: ScreenUtil().setWidth(14) / ScreenUtil().setSp(10)
        //                           ),),
        //                         )
        //                       ],
        //                     ),
        //                   ) ,
        //                 ) ,
        //                 Visibility(
        //                   visible: animation.value > ScreenUtil().setWidth(155) ,
        //                   child: Container(
        //                     width: ScreenUtil().setWidth(32),
        //                     height: ScreenUtil().setWidth(48),
        //                     margin: EdgeInsets.only(left:ScreenUtil().setWidth(61)),
        //                     child: Column(
        //                       children: <Widget>[
        //                         Container(
        //                           width: ScreenUtil().setWidth(32),
        //                           height: ScreenUtil().setWidth(32),
        //                           child: Image.asset("assets/images/homework.png"),
        //                         ) ,
        //                         Container(
        //                           height: ScreenUtil().setWidth(14),
        //                           child: Text("作业" , style: TextStyle(
        //                             fontSize: ScreenUtil().setWidth(10) ,
        //                             color: Color.fromARGB(255, 121, 132, 155) ,
        //                             height: ScreenUtil().setWidth(14) / ScreenUtil().setSp(10)
        //                           ),),
        //                         )
        //                       ],
        //                     ),
        //                   ) ,
        //                 ) ,
        //                 Visibility(
        //                   visible: animation.value > ScreenUtil().setWidth(250),
        //                   child: Container(
        //                     width: ScreenUtil().setWidth(32),
        //                     height: ScreenUtil().setWidth(48),
        //                     margin: EdgeInsets.only(left:ScreenUtil().setWidth(67)),
        //                     child: Column(
        //                       children: <Widget>[
        //                         Container(
        //                           width: ScreenUtil().setWidth(32),
        //                           height: ScreenUtil().setWidth(32),
        //                           child: Image.asset("assets/images/falg.png"),
        //                         ) ,
        //                         Container(
        //                           height: ScreenUtil().setWidth(14),
        //                           child: Text("Flag" , style: TextStyle(
        //                             fontSize: ScreenUtil().setWidth(10) ,
        //                             color: Color.fromARGB(255, 121, 132, 155) ,
        //                             height: ScreenUtil().setWidth(14) / ScreenUtil().setSp(10)
        //                           ),),
        //                         )
        //                       ],
        //                     ),
        //                   ) ,
        //                 ),
        //               ],
        //             ),
        //           ),
        //         ) ,
        Positioned(
          right: ScreenUtil().setWidth(15),
          bottom: ScreenUtil().setHeight(24),
          child: GestureDetector(
            child: Transform(
              transform: Matrix4.rotationZ(isSpread ? (3.14*3 / 4) / (289 / animation.value) : 0),
              origin: Offset(ScreenUtil().setWidth(28),ScreenUtil().setWidth(28)),
              child: Container(
                width: ScreenUtil().setWidth(56),
                height: ScreenUtil().setWidth(56),
                decoration: BoxDecoration(
                  color: Color.fromARGB(255, 0, 195, 206) , 
                  borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setWidth(56))) ,
                  boxShadow: [
                    BoxShadow(
                      color: Color.fromARGB(110, 0, 0, 0) ,
                      blurRadius: 3 ,
                      spreadRadius: 0
                    )
                  ] 
                ),
                child: Icon(Icons.add , color: Colors.white, size: ScreenUtil().setWidth(24),), 
              ),
            ),
            onTap: (){
              flag = true ;
              setState(() {
                content = "" ;
                mode = false ;
                Future.delayed(Duration(milliseconds: 500) , (){
                  FocusScope.of(context).requestFocus(focusNode) ;
                }) ;
              });
            },
          ),
        ) ,
        Positioned(
          bottom: 0,
          left: 0,
          child: Visibility(
            visible: flag,
            child: GestureDetector(
              child:Container(
                width: ScreenUtil().setWidth(375),
                height: ScreenUtil().setHeight(812),
                decoration: BoxDecoration(
                  color: Color.fromARGB(160 , 0, 0, 0) ,
                ),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end ,
                  children: <Widget>[
                    Container(
                      height: ScreenUtil().setHeight(306),
                      width: ScreenUtil().setWidth(375),
                      margin: EdgeInsets.only(bottom:ScreenUtil().setHeight(0)) ,
                      decoration: BoxDecoration(
                        color: Colors.white ,
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(16) ,
                          topRight: Radius.circular(16)
                        )
                      ),
                      child: GestureDetector(
                        child: Container(
                          color: Color.fromARGB(0, 0, 0, 0) ,
                          child: Column(
                            children: <Widget>[
                              Container(
                                height: ScreenUtil().setHeight(50),
                                child: Row(
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Container(
                                        color: Color.fromARGB(0, 0, 0, 0),
                                        width: ScreenUtil().setWidth(70),
                                        child: Center(
                                          child: Text("取消", style: TextStyle(
                                            fontSize: ScreenUtil().setSp(16) ,
                                            color: Color.fromARGB(255, 78, 86, 108)
                                          ),),
                                        ),
                                      ),
                                      onTap: (){
                                        setState(() {
                                          flag = false ;
                                        });
                                      },
                                    ) ,
                                    Expanded(
                                      child: Container(
                                        child: Center(
                                          child: Text("编辑Flag" , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(16) ,
                                            color: Color.fromARGB(255, 78, 86, 108) ,
                                            fontWeight: FontWeight.bold 
                                          ),),
                                        ),
                                      ),
                                    ) ,
                                    GestureDetector(
                                      child:Container(
                                        color: Color.fromARGB(0, 0, 0, 0),
                                        width: ScreenUtil().setWidth(70),
                                        child: Center(
                                          child: Text("确认" , style: TextStyle(
                                            fontSize: ScreenUtil().setSp(16) ,
                                            color: Color.fromARGB(255, 0, 167, 187)
                                          ),),
                                        ),
                                      ) ,
                                      onTap: (){
                                        setState(() {
                                          loading = true ;
                                        });
                                        if(content.trim() == ""){
                                          Toast.show("内容不能为空", context , gravity: 1) ;
                                          setState(() {
                                            loading = false ;
                                          });
                                        }else{
                                          if(!mode){
                                            UserService().addTasks(content).then((value){
                                              setState(() {
                                                loading = false ;
                                                flag = false ;
                                              });
                                              if(value["code"] == 200){
                                                print(value["data"]) ;
                                                flags.add({"id":value["data"].toString() , "content":content , "status":0 , "week":widget.week , "day":day}) ;
                                                Config().getSpf().setString("tasks", jsonEncode(flags)) ;
                                              }else{
                                                Toast.show(value["msg"], context , gravity: 1) ;
                                              }
                                            }) ;
                                          }else{
                                             UserService().updateTasks(content , flags[current]["id"].toString()).then((value){
                                              setState(() {
                                                loading = false ;
                                                flag = false ;
                                              });
                                              if(value["code"] == 200){
                                                flags[current]["content"] == content ;
                                                Config().getSpf().setString("tasks", jsonEncode(flags)) ;
                                              }else{
                                                Toast.show(value["msg"], context , gravity: 1) ;
                                              }
                                            }) ;
                                          }
                                        }
                                      },
                                    )
                                  ],
                                ),
                              ) ,
                              Container(
                                height:1 ,
                                color: Color.fromARGB(255, 236, 237, 241),
                              )  ,
                              Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(17) , right:ScreenUtil().setWidth(17) , top:ScreenUtil().setHeight(21)) ,
                                  child: TextField(
                                    focusNode: focusNode ,
                                    maxLines: 8 ,
                                    decoration: InputDecoration(
                                      border: InputBorder.none 
                                    ),
                                    controller:TextEditingController.fromValue(TextEditingValue(
                                      text: '${content == null ? "" : content}',  //判断keyword是否为空
                                      // 保持光标在最后

                                      selection: TextSelection.fromPosition(TextPosition(
                                          affinity: TextAffinity.downstream,
                                          offset: '${content}'.length))),
                                    ) ,
                                    onChanged: (value){
                                      content = value ;
                                    },
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                        onTap: (){

                        },
                      ),
                    )
                  ],
                ),
              ),
              onTap:(){
                setState(() {
                  flag = false ;
                });
              }
            ) ,
          ),
        )  ,
        Positioned(
          top:0 ,
          left:0 ,
          child: Visibility(
            visible: loading,
            child: Container(
              width: ScreenUtil().setWidth(375),
              height:ScreenUtil().setHeight(812) ,
              color: Color.fromARGB(180, 0, 0, 0),
              child: LoadingBouncingLine.circle() ,
            ) ,
          ),
        )
      ],
    );
  }

  tasks(index){
    if(flags[index]["status"] == 1){
      return flags[index]["week"] == widget.week && flags[index]["day"] == day ;
    }else{
      if(day < widget.today["weekDay"]){
        return false ;
      }else if(day == widget.today["weekDay"]){
        if(flags[index]["week"] == widget.week && flags[index]["day"] <= day){
          return true ;
        }else{
          return false ;
        }
      }else if(day > widget.today["weekDay"]){
        if(flags[index]["week"] == widget.week && flags[index]["day"] == day){
          return true ;
        }else{
          return false ;
        }
      }
    }
    // (flags[index]["status"] == 0 && (day <=  && widget.week == widget.today["week"] && day == widget.today["weekDay"])) 
    //                                   || ( && )
  }

  @override
  void dispose() {
    controller.dispose() ;
    super.dispose();
  }
}